package com.intelligent.health.setmeal.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.health.setmeal.dao.CheckGroupMapper;
import com.intelligent.health.setmeal.dto.CheckGroupDto;
import com.intelligent.health.setmeal.pojo.CheckItem;
import com.intelligent.health.setmeal.service.CheckGroupService;
import com.intelligent.health.setmeal.pojo.CheckGroup;
import com.intelligent.health.setmeal.vo.CheckGroupVo;
import entity.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/03/13/22:37
 * @Description:
 */
@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Override
    public PageResult<CheckGroupVo> queryCheckGroupsByPage(CheckGroupDto checkGroupDto, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper queryWrapper = getLambdaQueryWrapper(checkGroupDto);
        IPage<CheckGroup> checkGroups = checkGroupMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        PageResult<CheckGroupVo> pageResult = new PageResult<>();
        ArrayList<CheckGroupVo> checkGroupVos = new ArrayList<>();
        checkGroups.getRecords().forEach(checkGroup -> {
            CheckGroupVo checkGroupVo = new CheckGroupVo();
            BeanUtils.copyProperties(checkGroup, checkGroupVo);
            checkGroupVos.add(checkGroupVo);
        });
        pageResult.setTotal(checkGroups.getTotal());
        pageResult.setPageSize(checkGroups.getSize());
        pageResult.setCurrentPage(checkGroups.getCurrent());
        pageResult.setRows(checkGroupVos);
        return pageResult;
    }

    @Override
    public CheckGroupVo queryCheckGroupById(Integer checkGroupId) {
        CheckGroup checkGroup = checkGroupMapper.selectById(checkGroupId);
        CheckGroupVo checkGroupVo = new CheckGroupVo();
        BeanUtils.copyProperties(checkGroup, checkGroupVo);
        return checkGroupVo;
    }

    @Override
    public void addCheckGroup(HashMap<String, Object> hashMap) {
        CheckGroup checkGroup = JSONObject.parseObject(JSON.toJSONString(hashMap.get("checkGroup")), CheckGroup.class);
        //插入checkGroup
        checkGroupMapper.insert(checkGroup);
        //插入到中间表 t_checkgroup_checkitem
        ArrayList<Integer> checkItemIds = JSONObject.parseObject(JSON.toJSONString(hashMap.get("checkItemIds")), ArrayList.class);
        insertCheckItemToCheckGroup(checkGroup.getId(), checkItemIds);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void editCheckGroup(HashMap<String, Object> hashMap) {
        CheckGroup checkGroup = JSONObject.parseObject(JSON.toJSONString(hashMap.get("checkGroup")), CheckGroup.class);
        //插入checkGroup
        checkGroupMapper.updateById(checkGroup);
        //删除中间表中对于的数据
        checkGroupMapper.delCheckItemIdsByCheckGroupId(checkGroup.getId());
        //插入到中间表 t_checkgroup_checkitem
        ArrayList<Integer> checkItemIds = JSONObject.parseObject(JSON.toJSONString(hashMap.get("checkItemIds")), ArrayList.class);
        insertCheckItemToCheckGroup(checkGroup.getId(), checkItemIds);
    }

    @Override
    public void delBatchCheckGroup(ArrayList<Integer> checkGroupIds) {
        checkGroupMapper.delBatchCheckGroup(checkGroupIds);
    }

    @Override
    public List<CheckGroup> queryCheckGroups(HashMap<String, Object> hashMap) {
        return checkGroupMapper.queryCheckGroupsForExcel(hashMap);
    }

    @Override
    public List<CheckItem> queryCheckItemsByGid(Integer checkGroupId) {
        return checkGroupMapper.queryCheckItemsByGid(checkGroupId);
    }

    @Override
    public void delCheckGroupById(Integer checkGroupId) {
        checkGroupMapper.delCheckGroupById(checkGroupId);
    }

    /**
     * 将数据插入到中间表 t_checkgroup_checkitem
     *
     * @param checkGroupId
     * @param checkItemIds
     */
    public void insertCheckItemToCheckGroup(Integer checkGroupId, ArrayList<Integer> checkItemIds) {
        checkGroupMapper.insertBatchCheckItemToCheckGroup(checkGroupId, checkItemIds);
    }

    public static LambdaQueryWrapper getLambdaQueryWrapper(CheckGroupDto checkGroupDto) {
        LambdaQueryWrapper<CheckGroup> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(checkGroupDto.getCode())) {
            queryWrapper.like(CheckGroup::getCode, checkGroupDto.getCode());
        }
        if (StringUtils.isNotBlank(checkGroupDto.getName())) {
            queryWrapper.like(CheckGroup::getName, checkGroupDto.getName());
        }
        if (StringUtils.isNotBlank(checkGroupDto.getSex())) {
            queryWrapper.eq(CheckGroup::getSex, checkGroupDto.getSex());
        }
        if (StringUtils.isNotBlank(checkGroupDto.getHelpCode())) {
            queryWrapper.like(CheckGroup::getHelpCode, checkGroupDto.getHelpCode());
        }
        //前端默认有效 1
        if (checkGroupDto.getState() != null) {
            queryWrapper.eq(CheckGroup::getState, checkGroupDto.getState());
        }
        return queryWrapper;
    }
}
